﻿using MediatR;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;

namespace WechatMp.Web
{
    public class RequestActionFilter
    {
        private readonly IMediator _mediator;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mediator"></param>
        public RequestActionFilter(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var actionContext = await next();
            var httpContext = context.HttpContext;
            var httpRequest = httpContext.Request;
            var isRequestSucceed = actionContext.Exception == null; // 判断是否请求成功（没有异常就是成功）
            var headers = httpRequest.Headers;
            //var clientInfo = headers.ContainsKey("User-Agent") ? Parser.GetDefault().Parse(headers["User-Agent"]) : null;
            var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
            //var ip = httpContext.GetClientUserIp();
        }
    }
}
